package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.mapper.CustomerReportMapper;
import cn.wolfcode.crm.query.CustomerReportQuery;
import cn.wolfcode.crm.service.ICustomerReportService;
import cn.wolfcode.crm.util.JsonResult;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportServiceImpl;

    @Autowired
    private CustomerReportMapper customerReportMapper;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerReportQuery qo){
        PageInfo pageInfo = customerReportServiceImpl.selectByCustomerReport(qo);
        model.addAttribute("result",pageInfo);
        return "/customerReport/list";
    }

    @RequestMapping("/listByBar")
    public String listByBar(Model model, @ModelAttribute("qo") CustomerReportQuery qo){
        List<Map> list = customerReportMapper.selectByCustomerReport(qo);
        List xList = new ArrayList();
        List yList = new ArrayList();
        for (Map map : list) {
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        return "/customerReport/listByBar";
    }

    @RequestMapping("/listByPie")
    public String listByPie(Model model, @ModelAttribute("qo") CustomerReportQuery qo){
        List<Map> list = customerReportMapper.selectByCustomerReport(qo);
        List xList = new ArrayList();
        List yList = new ArrayList();

        for (Map map : list) {
            Map<String,Object> temp = new HashMap();
            xList.add(map.get("groupType"));
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("number"));
            yList.add(temp);
        }

        System.out.println(xList);
        System.out.println(JSON.toJSONString(yList));

        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        return "/customerReport/listByPie";
    }
}
